# -*- coding: iso-8859-1 -*-
#
#    This is part of Malvinas Argentinas Messenger
#
#    Malvinas Argentinas Messenger is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    Malvinas Argentinas Messenger is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Malvinas Argentinas Messenger; if not, write to the Free
#    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#    MA  02110-1301  USA

# List of contacts classes
class groupList:
	def __init__(self):
		self.grupos = []
	
	# Add a group to the groupList
	def appendGroup(self, item):		
		self.grupos.append(item)
	
	# Sort the list by name
	def sortl(self):
		self.grupos.sort(self.compare)

	# Function for the above sorting..
	def compare(self, x, y):
		if str(x.name) > str(y.name):
			return 1
		else:
			return -1

	# Add a passport inside the list of passports of the correct group.
	def appendpassport(self, passport):
		if self.size == 0:
			return 0		
		for i in self.grupos:
			if i.gid == passport.gid:
				i.passports.append(passport)
				return 1				
		return 0
	
	def size(self):
		return len(self.grupos)
	
	def numtoGroup(self, num):
	# First in list is 0
		if (num < self.size() + 1 ):
			return self.grupos[num].name
		else:
			return ''

	def numtoGroupName(self, num):
		for g in self.grupos:
			if g.gid == num:
				return g.name
		return ''		

	# Receives a group name and returns the gid if in list, else returns -1
	def groupinList(self, group):
		ans = -1
		for i in self.grupos:
			if i.name == group:
				ans = i.gid
				break
		return ans

	def userStatus(self, user):
		res = ''
		for g in self.grupos:
			for p in g.passports.passports:
				if p.name == user:
					res = p.status
					break
		return res
